import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Login from "@/views/Login";
import index from "@/views/admin/Index";
import index_user from "@/views/user/index";
import adduser from "@/views/admin/adduser";
import announcement from "@/views/admin/announcement";
import Book from "@/views/admin/Book";
import borrowing from "@/views/admin/borrowing";
import BookType from "@/views/admin/BookType";
import personal from "@/views/admin/personal";
import booklist from "v/user/booklist";
import Library from "v/user/Library";
import affiche from "v/user/affiche";


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path:'/index',
    name:'index',
    component: index,
    children:[
      {
        path: '/adduser',
        name: "adduser",
        component: adduser
      },
      {
        path: '/announcement',
        name: 'announcement',
        component: announcement
      },
      {
        path: '/Book',
        name: 'Book',
        component: Book
      },
      {
        path: '/borrowing',
        name: 'borrowing',
        component: borrowing
      },
      {
        path: '/BookType',
        name: 'BookType',
        component: BookType
      },
      {
        path: '/personal',
        name: 'personal',
        component: personal
      }
    ]
  },
  {
    path:'/index_user',
    name:'index_user',
    component: index_user,
    children: [
      {
        path: "/booklist",
        name:"booklist",
        component: booklist
      },
      {
        path: "/Library",
        name:"Library",
        component: Library
      },
      {
        path: "/affche",
        name:"affche",
        component: affiche
      }

    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  next()
  if(store.state.user==null){
    next({
      name: "login"
    })
  }else if(to.name=="login" && store.state.user) {
    next("/index")
  }else {
    next()
  }
})

export default router
